package com.example.spring_blog.controller;

import com.example.spring_blog.pojo.request.AddBlogRequest;
import com.example.spring_blog.pojo.request.UpdateBlogRequest;
import com.example.spring_blog.pojo.response.BlogInfoResponse;
import com.example.spring_blog.service.BlogService;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RequestMapping("/blog")
@RestController
public class BlogController {

    @Resource(name = "blogServiceImpl")
    private BlogService blogService;

    @RequestMapping("/getList")
    public List<BlogInfoResponse> getList() {
        log.info("获取主页所有博客列表...");
        List<BlogInfoResponse> blogs = blogService.getList();
        return blogs;
    }

    @RequestMapping("/getBlogsOfUser")
    public List<BlogInfoResponse> getBlogsOfUser(Integer userId) {
        log.info("获取用户：{} 的博客列表...", userId);
        List<BlogInfoResponse> blogsOfUser = blogService.getBlogsOfUser(userId);
        return blogsOfUser;
    }

    @RequestMapping("/getBlogDetail")
    // @NotNull 是validation注解，表示blogId不能为null
    public BlogInfoResponse getBlogDetail(@NotNull Integer blogId) {
        log.info("获取博客详情，blogId:{}",blogId);
        return blogService.getBlogDetail(blogId);
    }

    @RequestMapping("/add")
    public Boolean addBlog(@Validated @RequestBody AddBlogRequest addBlogRequest) {
        log.info("添加博客，userId: {}，title: {}", addBlogRequest.getUserId(), addBlogRequest.getTitle());
        return blogService.addBlog(addBlogRequest);
    }

    @RequestMapping("/update")
    public Boolean updateBlog(@Validated @RequestBody UpdateBlogRequest updateBlogRequest) {
        log.info("修改博客，博客id：{}", updateBlogRequest.getId());
        return blogService.updateBlog(updateBlogRequest);
    }

    @RequestMapping("/delete")
    public Boolean deleteBlog(@NotNull(message = "博客id不能为空") Integer blogId) {
        log.info("删除博客，博客id：{}", blogId);
        return blogService.deleteBlog(blogId);
    }
}
